package com.dengqi.mymodule.activity.network.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dengqi.mymodule.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author: dengqi
 * @CreateDate: 2020/10/19 17:31
 * @Description:
 */
public abstract class BaseAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

    private List<T> data = new ArrayList<>();
    protected OnItemClickListener<T> onItemClickListener;//单击事件

    public void setData(List<T> data){
        if(data!=null){
            this.data = data;
        }
    }

    public void addData(List<T> data){
        if(data!=null){
            this.data.addAll(data);
        }
    }

    public void clearData(){
        this.data.clear();
    }

    public abstract VH onSubCreateViewHolder(ViewGroup parent, int viewType);
    public abstract void onSubBindViewHolder(T object, VH holder, int position);

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return onSubCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        T object = data.get(position);
        onSubBindViewHolder(object, holder, position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(View v, int position, T mode);
    }

}
